
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.FAQListItem;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

/**
 * 描述:获取FAQ详情请求类
 *
 * @author jamywang
 * @since 2015年11月17日 下午3:45:46
 */
public class FAQDetailTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public FAQDetailHttpResponse request(int titleId) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_FAQ_DETAIL);
        params.put("id", titleId);
        // 生成请求包
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        // 生成响应包
        ResponsePackage responsePackage = new FAQDetailResponsePackage();
        // 生成响应类
        FAQDetailHttpResponse appsHttpResponse = new FAQDetailHttpResponse();
        requestParams.add(params);
        requestPackage.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_FAQ_DETAIL);
        try {
            KGHttpClient.request(requestPackage, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;

    }

    class FAQDetailResponsePackage extends HttpBaseResponsePackage<FAQDetailHttpResponse> {

        @Override
        protected void handleResponse(FAQDetailHttpResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "FAQDetailTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    response.setCode(jsonResp.optString("code"));
                    response.setMessage(jsonResp.optString("message"));
                    response.setPrompt(jsonResp.optString("prompt"));
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_FAQ_DETAIL.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            FAQListItem faqItem = new FAQListItem();
                            faqItem.setTitle(jsonResult.getString("title"));
                            faqItem.setContent(jsonResult.getString("content"));
                            faqItem.setInsertTime(jsonResult.getInt("inserttime"));
                            faqItem.setItemId(jsonResult.getInt("id"));
                            response.setFAQDetail(faqItem);
                        }
                    }
                }
                response.setOk(true);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

    public class FAQDetailHttpResponse extends ResponseMessage {

        private FAQListItem itemDetail;

        public FAQListItem getFAQDetail() {
            return itemDetail;
        }

        public void setFAQDetail(FAQListItem itemDetail) {
            this.itemDetail = itemDetail;
        }

    }

}
